Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./year2.RDS")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2021-06-30"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2021-06-30"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.25, n = 316)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 11.53129 11.56980 11.60797 11.64581 11.68331 11.72049 11.75734 11.79389
##   [9] 11.83012 11.86605 11.90168 11.93701 11.97205 12.00681 12.04126 12.07542
##  [17] 12.10926 12.14278 12.17598 12.20886 12.24140 12.27360 12.30564 12.33763
##  [25] 12.36952 12.40124 12.43273 12.46393 12.49478 12.52521 12.55516 12.58457
##  [33] 12.61337 12.64150 12.66963 12.69818 12.72676 12.75500 12.78251 12.80890
##  [41] 12.83378 12.85916 12.88673 12.91558 12.94480 12.97350 13.00076 13.02570
##  [49] 13.04739 13.06495 13.08048 13.09640 13.11232 13.12783 13.14250 13.15594
##  [57] 13.16772 13.17746 13.18472 13.18911 13.19021 13.18761 13.18068 13.16951
##  [65] 13.15481 13.13727 13.11758 13.09644 13.07456 13.05262 13.03133 13.00598
##  [73] 12.97313 12.93538 12.89534 12.85561 12.81879 12.78747 12.75758 12.72384
##  [81] 12.68712 12.64828 12.60817 12.56766 12.52760 12.48884 12.45225 12.41869
##  [89] 12.38900 12.36406 12.34243 12.32208 12.30293 12.28491 12.26795 12.25198
##  [97] 12.23693 12.22273 12.20930 12.19765 12.18858 12.18175 12.17679 12.17338
## [105] 12.17114 12.16974 12.16882 12.16803 12.16702 12.16545 12.16295 12.16008
## [113] 12.15766 12.15573 12.15438 12.15365 12.15362 12.15436 12.15578 12.15776
## [121] 12.16024 12.16320 12.16660 12.17039 12.17454 12.17900 12.18374 12.18984
## [129] 12.19792 12.20718 12.21686 12.22618 12.23436 12.24063 12.24591 12.25155
## [137] 12.25746 12.26351 12.26960 12.27563 12.28147 12.28703 12.29219 12.29684
## [145] 12.30088 12.30419 12.30394 12.29849 12.28951 12.27867 12.26764 12.25809
## [153] 12.25170 12.25013 12.25506 12.26369 12.27223 12.28088 12.28984 12.29930
## [161] 12.30946 12.32052 12.33268 12.34613 12.36107 12.37771 12.39623 12.42023
## [169] 12.45231 12.49111 12.53528 12.58349 12.63438 12.68661 12.73883 12.78970
## [177] 12.83787 12.88200 12.92074 12.95274 12.97667 12.99746 13.02051 13.04509
## [185] 13.07050 13.09603 13.12097 13.14461 13.16623 13.18514 13.20061 13.21193
## [193] 13.21841 13.21932 13.21396 13.19985 13.17674 13.14719 13.11380 13.07916
## [201] 13.04584 13.01643 12.98408 12.94183 12.89211 12.83735 12.77999 12.72247
## [209] 12.66721 12.61666 12.57324 12.53273 12.48961 12.44443 12.39773 12.35006
## [217] 12.30197 12.25398 12.20666 12.16053 12.11615 12.07406 12.03480 11.99716
## [225] 11.95968 11.92253 11.88589 11.84994 11.81484 11.78076 11.74789 11.71639
## [233] 11.68382 11.64864 11.61236 11.57651 11.54258 11.51211 11.48661 11.46433
## [241] 11.44268 11.42185 11.40205 11.38348 11.36634 11.35084 11.33716 11.32553
## [249] 11.31613 11.30917 11.30485 11.30230 11.30068 11.30025 11.30128 11.30402
## [257] 11.30874 11.31570 11.32515 11.33736 11.35479 11.37896 11.40872 11.44295
## [265] 11.48050 11.52024 11.56102 11.60172 11.64119 11.67829 11.71189 11.74085
## [273] 11.77179 11.80989 11.85188 11.89446 11.93436 11.96830 11.99299 12.00973
## [281] 12.02259 12.03247 12.04024 12.04680 12.05304 12.05982 12.06805 12.07861
## [289] 12.08983 12.09966 12.10846 12.11655 12.12429 12.13201 12.14005 12.14803
## [297] 12.15536 12.16206 12.16815 12.17368 12.17866 12.18312 12.18709 12.19060
## [305] 12.19367 12.19634 12.19862 12.20063 12.20244 12.20401 12.20530 12.20626
## [313] 12.20687 12.20707 12.20684 12.20612
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./site_objects/wrf_a_year2.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.25, n = 316)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 10.71289 10.80583 10.89688 10.98602 11.07325 11.15856 11.24191 11.32332
##   [9] 11.40275 11.48020 11.55565 11.62910 11.70052 11.76989 11.83721 11.90251
##  [17] 11.96583 12.02721 12.08668 12.14429 12.20007 12.25406 12.30596 12.35552
##  [25] 12.40284 12.44802 12.49115 12.53234 12.57168 12.60928 12.64523 12.67964
##  [33] 12.71260 12.74421 12.77393 12.80133 12.82666 12.85019 12.87217 12.89285
##  [41] 12.91250 12.92947 12.94240 12.95203 12.95907 12.96424 12.96826 12.97187
##  [49] 12.97578 12.98071 12.98520 12.98750 12.98793 12.98681 12.98445 12.98119
##  [57] 12.97734 12.97320 12.96912 12.96539 12.96235 12.96031 12.95851 12.95603
##  [65] 12.95293 12.94927 12.94511 12.94051 12.93553 12.93024 12.92469 12.91981
##  [73] 12.91602 12.91258 12.90880 12.90395 12.89731 12.88817 12.87715 12.86536
##  [81] 12.85285 12.83966 12.82583 12.81139 12.79640 12.78089 12.76490 12.74847
##  [89] 12.73165 12.71447 12.69698 12.67916 12.66092 12.64219 12.62288 12.60291
##  [97] 12.58220 12.56068 12.53826 12.51386 12.48674 12.45733 12.42608 12.39342
## [105] 12.35979 12.32562 12.29134 12.25741 12.22425 12.19229 12.16198 12.12598
## [113] 12.07981 12.02822 11.97594 11.92774 11.88835 11.86251 11.84379 11.82333
## [121] 11.80232 11.78189 11.76321 11.74744 11.73574 11.72926 11.72917 11.73627
## [129] 11.74942 11.76682 11.78671 11.80727 11.82674 11.84331 11.86136 11.88569
## [137] 11.91518 11.94874 11.98523 12.02356 12.06261 12.10126 12.13841 12.17295
## [145] 12.20375 12.22971 12.25568 12.28638 12.32054 12.35685 12.39403 12.43080
## [153] 12.46586 12.49792 12.52570 12.54958 12.57110 12.59064 12.60859 12.62535
## [161] 12.64131 12.65686 12.67240 12.68831 12.70499 12.72284 12.74224 12.76420
## [169] 12.78909 12.81639 12.84557 12.87610 12.90743 12.93905 12.97042 13.00101
## [177] 13.03028 13.05771 13.08277 13.10491 13.12362 13.14126 13.16025 13.18011
## [185] 13.20040 13.22064 13.24038 13.25914 13.27647 13.29190 13.30497 13.31521
## [193] 13.32217 13.32538 13.32436 13.31941 13.31140 13.30081 13.28812 13.27380
## [201] 13.25832 13.24215 13.22424 13.20346 13.18018 13.15474 13.12750 13.09882
## [209] 13.06905 13.03854 13.00767 12.97500 12.93915 12.90052 12.85952 12.81655
## [217] 12.77201 12.72631 12.67984 12.63301 12.58623 12.53989 12.49440 12.44461
## [225] 12.38676 12.32334 12.25683 12.18972 12.12450 12.06365 12.00966 11.96502
## [233] 11.92732 11.89212 11.85878 11.82671 11.79528 11.76388 11.73189 11.69955
## [241] 11.66762 11.63632 11.60586 11.57646 11.54833 11.52169 11.49674 11.47370
## [249] 11.45278 11.43420 11.41816 11.40505 11.39483 11.38711 11.38150 11.37762
## [257] 11.37508 11.37348 11.37245 11.37159 11.37056 11.36948 11.36866 11.36839
## [265] 11.36898 11.37073 11.37395 11.37892 11.38596 11.39537 11.40745 11.42249
## [273] 11.44530 11.47804 11.51703 11.55863 11.59918 11.63502 11.66249 11.68604
## [281] 11.71208 11.73989 11.76877 11.79800 11.82687 11.85469 11.88072 11.90427
## [289] 11.92659 11.94927 11.97212 11.99496 12.01759 12.03981 12.06144 12.08274
## [297] 12.10404 12.12530 12.14649 12.16756 12.18846 12.20915 12.22959 12.24974
## [305] 12.26955 12.28897 12.30798 12.32659 12.34488 12.36288 12.38061 12.39809
## [313] 12.41533 12.43238 12.44923 12.46593
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./site_objects/wrf_b_year2.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.25, n = 316)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 10.82095 10.88638 10.95057 11.01351 11.07516 11.13550 11.19449 11.25210
##   [9] 11.30831 11.36309 11.41640 11.46822 11.51853 11.56732 11.61468 11.66065
##  [17] 11.70527 11.74859 11.79067 11.83154 11.87127 11.90989 11.94666 11.98099
##  [25] 12.01313 12.04334 12.07188 12.09901 12.12499 12.15007 12.17451 12.19858
##  [33] 12.22252 12.24660 12.26977 12.29114 12.31117 12.33036 12.34919 12.36814
##  [41] 12.38769 12.40844 12.43033 12.45286 12.47552 12.49781 12.51921 12.53923
##  [49] 12.55735 12.57307 12.58724 12.60096 12.61406 12.62640 12.63780 12.64812
##  [57] 12.65720 12.66487 12.67099 12.67540 12.67794 12.67845 12.67682 12.67325
##  [65] 12.66803 12.66144 12.65377 12.64530 12.63634 12.62715 12.61803 12.60713
##  [73] 12.59301 12.57658 12.55876 12.54046 12.52259 12.50607 12.48897 12.46911
##  [81] 12.44698 12.42307 12.39787 12.37189 12.34562 12.31955 12.29417 12.26999
##  [89] 12.24749 12.22717 12.20676 12.18423 12.16041 12.13612 12.11220 12.08948
##  [97] 12.06878 12.05095 12.03681 12.02580 12.01664 12.00906 12.00281 11.99761
## [105] 11.99320 11.98933 11.98573 11.98215 11.97830 11.97394 11.96881 11.96526
## [113] 11.96501 11.96676 11.96925 11.97120 11.97133 11.96837 11.96348 11.95874
## [121] 11.95420 11.94993 11.94598 11.94243 11.93932 11.93674 11.93472 11.93379
## [129] 11.93401 11.93481 11.93560 11.93582 11.93488 11.93220 11.92656 11.91758
## [137] 11.90596 11.89240 11.87761 11.86229 11.84713 11.83286 11.82016 11.80974
## [145] 11.80229 11.79854 11.79509 11.78889 11.78110 11.77286 11.76535 11.75972
## [153] 11.75714 11.75875 11.76572 11.77690 11.79024 11.80552 11.82255 11.84112
## [161] 11.86104 11.88210 11.90411 11.92686 11.95014 11.97377 11.99753 12.02465
## [169] 12.05786 12.09618 12.13867 12.18436 12.23230 12.28152 12.33106 12.37998
## [177] 12.42730 12.47208 12.51334 12.55014 12.58151 12.61213 12.64667 12.68420
## [185] 12.72378 12.76447 12.80533 12.84543 12.88383 12.91959 12.95176 12.97942
## [193] 13.00163 13.01744 13.02591 13.02738 13.02358 13.01570 13.00493 12.99245
## [201] 12.97945 12.96712 12.95259 12.93284 12.90885 12.88158 12.85199 12.82106
## [209] 12.78975 12.75902 12.72984 12.69883 12.66252 12.62181 12.57756 12.53065
## [217] 12.48196 12.43236 12.38273 12.33395 12.28689 12.24243 12.20144 12.15861
## [225] 12.10943 12.05602 12.00050 11.94496 11.89154 11.84232 11.79944 11.76500
## [233] 11.73531 11.70578 11.67719 11.65029 11.62585 11.60462 11.58737 11.57574
## [241] 11.57023 11.56990 11.57382 11.58104 11.59064 11.60167 11.61320 11.62429
## [249] 11.63400 11.64140 11.64554 11.65162 11.66421 11.68141 11.70136 11.72219
## [257] 11.74200 11.75893 11.77110 11.77664 11.77550 11.76955 11.75974 11.74703
## [265] 11.73236 11.71671 11.70102 11.68624 11.67334 11.66326 11.65696 11.65541
## [273] 11.65434 11.65005 11.64432 11.63890 11.63556 11.63607 11.64220 11.65470
## [281] 11.67227 11.69363 11.71749 11.74255 11.76752 11.79112 11.81205 11.82902
## [289] 11.84480 11.86254 11.88153 11.90104 11.92038 11.93881 11.95562 11.97165
## [297] 11.98817 12.00508 12.02230 12.03973 12.05730 12.07491 12.09248 12.10992
## [305] 12.12715 12.14407 12.16060 12.17659 12.19201 12.20695 12.22148 12.23571
## [313] 12.24971 12.26357 12.27737 12.29122
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./site_objects/wrf_c_year2.rda")

keeping in case

#save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
#save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
#save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
#save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
#save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
#save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
#save(both_ymina, file = "./plotly_objs/both_ymina.rda")
#save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

#save(both_yminb, file = "./plotly_objs/both_yminb.rda")
#save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

#save(both_yminc, file = "./plotly_objs/both_yminc.rda")
#save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")